﻿/// <reference name="MicrosoftAjax.js"/>


Type.registerNamespace("MikeKappel.Com.Uploader");

MikeKappel.Com.Uploader.ProgressBar = function(element) {
MikeKappel.Com.Uploader.ProgressBar.initializeBase(this, [element]);
}

MikeKappel.Com.Uploader.ProgressBar.prototype = {
    initialize: function() {
        MikeKappel.Com.Uploader.ProgressBar.callBaseMethod(this, 'initialize');
        this.id = this.get_Guid();
        this.container = document.getElementById(this.get_ControlId());
        this.iFrame = this.container.getElementsByTagName('iframe')[0];
        this.progressContainer = this.container.getElementsByTagName('div')[0];
        this.progressBar = this.progressContainer.getElementsByTagName('div')[0];
        MikeKappel.Com[this.get_Name()] = this;
    },
    dispose: function() {
        //Add custom dispose actions here
        MikeKappel.Com.Uploader.ProgressBar.callBaseMethod(this, 'dispose');
    },

    // ImageKey Property Accessors
    get_ImageKey: function() {
        return this._imageKey;
    },
    // ImageKey Property Accessors
    set_ImageKey: function(value) {
        this._imageKey = value;
    },

    // Name Property Accessors
    get_Name: function() {
        return this._Name;
    },
    set_Name: function(value) {
        this._Name = value;
    },
    // Progress Url Property Accessors
    get_UrlOfForm: function() {
        return this._UrlOfForm;
    },
    set_UrlOfForm: function(value) {
        this._UrlOfForm = value;
    },
    // Progress Url Property Accessors
    get_UrlOfProgress: function() {
        return this._UrlOfProgress;
    },
    set_UrlOfProgress: function(value) {
        this._UrlOfProgress = value;
    },
    // Progress Url Property Accessors
    get_UrlOfCancel: function() {
        return this._UrlOfCancel;
    },
    set_UrlOfCancel: function(value) {
        this._UrlOfCancel = value;
    },

    // Image Url Property Accessors
    get_UrlOfImage: function() {
        return this._UrlOfImage;
    },
    set_UrlOfImage: function(value) {
        this._UrlOfImage = value;
    },

    // Thumbnail Url Property Accessors
    get_UrlOfThumbnail: function() {
        return this._UrlOfThumbnail;
    },
    set_UrlOfThumbnail: function(value) {
        this._UrlOfThumbnail = value;
    },

    // Control Id Property Accessors
    get_ControlId: function() {
        return this._ControlId;
    },
    set_ControlId: function(value) {
        this._ControlId = value;
    },

    // Name Property Accessors
    get_Guid: function() {
        return this._guid;
    },
    set_Guid: function(value) {
        this._guid = value;
    },
    upload_Cancel: function() {
        var myrequest = new Sys.Net.WebRequest();
        myrequest.set_url(this.get_UrlOfCancel());
        myrequest.set_httpVerb('GET');
        myrequest.add_completed(this.upload_Cancelled);
        myrequest.set_userContext(this.get_Name());
        myrequest.invoke();
    },
    get_Progress: function() {
        var myrequest = new Sys.Net.WebRequest();
        myrequest.set_url(this.get_UrlOfProgress());
        myrequest.set_httpVerb('GET');
        myrequest.add_completed(this.got_Progress);
        myrequest.set_userContext(this.get_Name());
        myrequest.invoke();

    },
    got_Progress: function(executor, eventArgs) {
        if (executor.get_responseAvailable()) {
            var obj = MikeKappel.Com[executor.get_webRequest().get_userContext()];
            obj.progress = executor.get_responseData();
            if (obj.progress == 'false') {
                obj.progressBar.className = 'ProgressBarError';
                obj.progressBar.style.width = '100%';
            } else if (obj.progress < 100) {
                obj.progressBar.style.width = obj.progress + '%';
                var toCheckProgress = setTimeout('MikeKappel.Com.' + obj.get_Name() + '.get_Progress();', 250);
            } else {
                obj.progressBar.style.width = '100%';
            }
        }
        else {
            if (executor.get_timedOut())
                alert("Timed Out");
            else
                if (executor.get_aborted())
                alert("Aborted");
        }
    },
    upload_Cancelled: function(executor, eventArgs) {
        if (executor.get_responseAvailable()) {
            var obj = MikeKappel.Com[executor.get_webRequest().get_userContext()];
            obj.progress = executor.get_responseData();
            if (obj.progress) {
                obj.progressBar.style.width = '100%';
                obj.progressBar.style.backgroundColor = '#777777';
                obj.iFrame.src = obj.get_UrlOfForm();
                alert(obj.progress);
            } else {
                alert('Error in AjaxUploader.js upload_Cancelled');
            }
        }
        else {
            if (executor.get_timedOut())
                alert("Timed Out");
            else
                if (executor.get_aborted())
                alert("Aborted");
        }
    }
}
MikeKappel.Com.Uploader.ProgressBar.registerClass('MikeKappel.Com.Uploader.ProgressBar', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

function UploadStared(sender) {
    MikeKappel.Com[sender].progressBar.className = 'ProgressBar';
    var toStartUploadCheck = setTimeout('MikeKappel.Com.' + sender + '.get_Progress();', 250);
    return true;
}
function UploadCancel(sender) {
    MikeKappel.Com[sender].upload_Cancel();
    return false;
}
function FormLoaded(sender) {
    var toStartUploadCheck = setTimeout('MikeKappel.Com.' + sender + '.progressBar.style.width = \'0px\'', 500);
}
